package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.PermissionMapper;
import com.itheima.mapper.RoleMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.List;

@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public User findUsername(String username) {
        return userMapper.findUsername(username);
    }

    @Override
    public PageResult queryUserByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);
        List<User> list = userMapper.queryAllUser(queryString);
        PageInfo<User> pageinfo = new PageInfo<>(list);

        return new PageResult(pageinfo.getTotal(),pageinfo.getList());
    }



    @Override
    public void addUser(User user, Integer[] permissionIds, Integer[] roleIds) {

        String password = user.getPassword();
        user.setPassword(passwordEncoder.encode(password));
        userMapper.addUser(user);

        addUserIdAndRoleId(roleIds,user.getId());
        addRoleIdAndPermissionId(permissionIds,roleIds);

    }

    @Override
    public Integer[] queryPermissionIdsByRoleId(Integer userId) {
        Integer[] roleIds = userMapper.queryRoleIdsByUserId(userId);

        return queryPermissionIdsByRoleIds(roleIds);
    }

    private Integer[] queryPermissionIdsByRoleIds(Integer[] roleIds) {
        if (roleIds != null && roleIds.length > 0) {
            for (Integer roleId : roleIds) {
                return roleMapper.queryPermissionIdsByRoleId(roleId);
            }
        }
        return null;
    }

    @Override
    public Integer[] queryRoleIdsByUserId(Integer userId) {
        return userMapper.queryRoleIdsByUserId(userId);
    }

    private void addRoleIdAndPermissionId(Integer[] permissionIds, Integer[] roleIds) {

        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer roleId : roleIds) {
                roleMapper.addRoleIdAndPermissionId(permissionIds, roleId);
            }
        }
    }

    private void addUserIdAndRoleId(Integer[] roleIds, Integer id) {

        if (roleIds != null && roleIds.length > 0) {
            userMapper.addUserIdAndRoleId(roleIds,id);
        }
    }

    @Override
    public void updateUsers(User user, Integer[] permissionIds, Integer[] roleIds) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        userMapper.updateUsers(user);
        userMapper.deleteRoleIdByUserId(user.getId());
        userMapper.addUserIdAndRoleId(roleIds,user.getId());
        deletePermissionIdByRoleId(roleIds);
        addRoleIdAndPermissionId(permissionIds,roleIds);


    }

    private void deletePermissionIdByRoleId(Integer[] roleIds) {

        for (Integer roleId : roleIds) {
            roleMapper.deletePermissionIdByRoleId(roleId);
        }
    }

    @Override
    public void deleteUserById(Integer id) {

        Integer[] roleIds = userMapper.queryRoleIdsByUserId(id);
        deletePermissionIdByRoleId(roleIds);
        userMapper.deleteRoleIdByUserId(id);
        userMapper.deleteUserById(id);

    }
}
